package mannA1.model;

import java.util.ArrayList;
import mannA1.view.A1GUI;

public class Model 
{

	public int numLines;
	public int numRounds;
	public int numEdges;
	public int finalNumEdges;
	public Line[] lines;
	public ArrayList<Edge> edges;
	int pixeldiff = 7;
	public String[] finalChoices;

	String choice = "";

	A1GUI a1gui;

	
	/** Constructor for my Model class.
	 * 
	 * @param numLines		the number of lines in the event, as specified by the user.
	 * @param numRounds		the number of rounds in the event, as specified by the user.
	 * @param lines			an array of lines, for the model to reference.
	 * @param edges			ArrayList of edges, to keep track of all the edges created.
	 * @param numEdges		the number of edges to be added, numLines*numRounds.
	 * */
	public Model (int numLines, int numRounds, Line[] lines, ArrayList<Edge> edges, int numEdges)
	{
		this.numLines = numLines;
		this.numRounds = numRounds;
		this.lines = lines;
		this.edges = edges;
		this.numEdges = numEdges;
		this.finalChoices = new String[numLines];
		
		//lines starts from 0
		for (int i = 0; i < numLines; i++)
		{
			lines[i] = new Line(choice, i, edges);
		}
		finalNumEdges = numEdges;
	}

	/** checks that the edge to be added is valid. */
	public boolean addEdge(int firstLine, int secondLine, int height)
	{
		if (isValidEdge(firstLine, secondLine, height, edges) == false)
		{
			System.out.println("Edge is invalid, because it is within 7 pixels of another Edge");
			return false;
		}
		return true;
	}

	/** Checks that an edge is not too close to another edge. */
	public boolean isValidEdge(int firstLine, int secondLine, int height, ArrayList<Edge> edges)
	{
		for (int i = 0; i < edges.size(); i++)
		{
			if (Math.abs(edges.get(i).height - height) <= pixeldiff)
			{
				if (edges.get(i).leftChoice == secondLine || edges.get(i).rightChoice == firstLine)
				{
					return false;
				}
			}
		}
		return true;
	}

	/** Called when the last line is added, to move around the choices.*/
	public void isDone()
	{
		//end decision lines event, print out final lists, should call prioritized choices
		PrioritizedChoices();
	}

	/** Logic for switching choices between lines, stored in the array choices. */
	public void PrioritizedChoices()
	{
		//set up array of finalChoices
		for (int f = 0; f < numLines; f++)
		{	
			finalChoices[f] = lines[f].choice; 
		}
		
		//find highest edge, switch choices
		for (int fne = 0; fne < finalNumEdges; fne++)
		{
			/*
			System.out.println("Left choice is: " + finalChoices[edges.get(fne).leftChoice]);
			System.out.println("Right choice is: " + finalChoices[edges.get(fne).rightChoice]);*/
			
			String placeholder = finalChoices[edges.get(fne).leftChoice];
			finalChoices[edges.get(fne).leftChoice] = finalChoices[edges.get(fne).rightChoice];
			finalChoices[edges.get(fne).rightChoice] = placeholder;
			/*
			System.out.println("Left choice is: " + finalChoices[edges.get(fne).leftChoice]);
			System.out.println("Right choice is: " + finalChoices[edges.get(fne).rightChoice]);
			
			System.out.println("__________");*/
		}		
		
		/** prints out the final list of choices. */
		System.out.println("Order of choices, first to last:");
		for(int i = 0; i < numLines; i++)
		{
			System.out.println(finalChoices[i]);
		}
	}

	/** choose where to place the edge based on height*/
	public void sortEdge(Edge e)
	{
		if (edges.size() == 0)
		{
			edges.add(e);
			return;
		}

		for (int current = 0; current < edges.size(); current++)
		{
			if (e.height <= edges.get(current).height)
			{
				edges.add(current, e);
				return;
			}
		}		
		edges.add(e);
		return;
	}
	
	public int getNumLines()
	{
		return numLines;
	}
}